package com.gzsxy.utils;

import java.util.Random;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 获取16位纯数字字符串 且字符串第一位不为0
 * 注意：该算法能保证生成的随机数不重复，当每秒的并发量为1000
 * @date 2021/10/8 16:24
 */
public class RandomUtil {


    public static String randomInt16() {
        String target = null;
        synchronized (RandomUtil.class) {
            try {
                TimeUnit.MILLISECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            target = "" + (new Random().nextInt(900) + 100) + System.currentTimeMillis();
        }
        if (target.length() > 16) {
            target = target.substring(0, 16);
        }
        String[] arr = new String[8];
        for (int i = 0; i < 8; i++) {
            arr[i] = target.substring(2 * i, 2 * i + 2);
        }
        return arr[0] + arr[5] + arr[3] + arr[4] + arr[2] + arr[7] + arr[1] + arr[6];
    }


    public static void main(String[] args) {
        long begin = System.currentTimeMillis();
        TreeSet<String> set = new TreeSet<>();
        for (int i = 0; i < 10000; i++) {
            set.add(randomInt16());
        }
        long end = System.currentTimeMillis();
        System.out.println(set.size());
        System.out.println(end - begin);
    }
}
